package cn.lopr.hfbdqn.service;

import cn.lopr.hfbdqn.common.Result;
import cn.lopr.hfbdqn.dao.CommentMapper;
import cn.lopr.hfbdqn.dao.ImgMapper;
import cn.lopr.hfbdqn.dao.MovieMapper;
import cn.lopr.hfbdqn.pojo.Img;
import cn.lopr.hfbdqn.pojo.Movie;
import cn.lopr.hfbdqn.route.MovieRoute;
import cn.lopr.hfbdqn.vo.MovieVo;
import cn.lopr.mvc.frame.annotaion.Autowired;
import cn.lopr.mvc.frame.annotaion.Service;
import cn.lopr.util.ListUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author shuangyang7
 * @date 2021/7/19 11:03
 */
@Service
public class MovieService implements MovieRoute {

    @Autowired
    MovieMapper movieMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    ImgMapper imgMapper;

    public Result<?> getAll() {
        return null;
    }

    @Override
    public Result<?> getByHotComment() {
        List<Integer> movieIds = commentMapper.selectMovieIdByCommentNum();
        List<Movie> movies = movieMapper.selectMovieByIds(movieIds);
        return new Result<>(Result.SUCCESS, movies);
    }

    @Override
    public Result<?> getByType(Integer type) {
        List<Movie> movies = new ArrayList<>();
        if (type == 1) {
            List<Integer> movieIds = commentMapper.selectMovieIdByCommentNum();
            movies = movieMapper.selectMovieByIds(movieIds);
        }
        if (type == 2) {
            movies = movieMapper.selectByMovieExpectationValue();
        }
        if (type == 3) {
            movies = movieMapper.selectByMovieUmulativeBoxOffice();
        }

        return new Result<>(Result.SUCCESS, movies);
    }

    @Override
    public Result<Movie> getById(Integer id) {
        Movie movie = movieMapper.selectByPrimaryKey(id);
        MovieVo movieVo = new MovieVo(movie);
        Float avgScore = commentMapper.selectAvgScoreByMovieId(id);
        movieVo.setMovieScore(avgScore);

        return new Result<>(Result.SUCCESS, movieVo);
    }

    @Override
    public Result<?> getByTypeAndPage(Integer type, Integer page, Integer size) {

        List<Movie> movies = new ArrayList<>();

        if (type == 1) {
            PageHelper.startPage(page, size);
            List<Integer> movieIds = commentMapper.selectMovieIdByCommentNumAndPage();
            if (!ListUtils.isEmpty(movieIds)) {
                movies = movieMapper.selectMovieByIds(movieIds);
            }
        } else if (type == 2) {
            PageHelper.startPage(page, size);
            movies = movieMapper.selectByMovieExpectationValueAndPage();
        } else if (type == 3) {
            PageHelper.startPage(page, size);
            movies = movieMapper.selectByMovieUmulativeBoxOfficeAndPage();
        }
        PageInfo<Movie> moviePageInfo = new PageInfo<>(movies);
        return new Result<>(Result.SUCCESS, moviePageInfo);
    }

    @Override
    public Result<?> getMovieImagesById(Integer id) {
        List<Img> scrollImg = imgMapper.selectByEnableAndNotDeleteAndMovieIdAndTypeScroll(id);
        List<Img> bannerImg = imgMapper.selectByEnableAndNotDeleteAndMovieIdAndTypeBanner(id);
        Stream<Img> sorted = scrollImg.stream().sorted(Comparator.comparing(Img::getImgSort));

        Movie movie = movieMapper.selectByPrimaryKey(id);
        MovieVo movieVo = new MovieVo(movie);
        Float avgScore = commentMapper.selectAvgScoreByMovieId(id);
        movieVo.setMovieScore(avgScore);
        if (!ListUtils.isEmpty(bannerImg)) {
            movieVo.setImgBasePath(bannerImg.get(0).getImgBasePath());
            movieVo.setMovieManPicture(bannerImg.get(0).getImgBasePath());
        }

        return new Result<>(Result.SUCCESS, new HashMap<String, Object>() {{
            put("3", sorted.toArray());
            put("4", movieVo);
        }});
    }

    @Override
    public Result<?> search(String s, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Movie> movies = movieMapper.search(s);
        return new Result<>(Result.SUCCESS, movies);
    }

    @Override
    public Result<?> requestBodyTest(Movie movie) {
        return new Result<>(Result.SUCCESS, movie);
    }


}
